package com.cn.kabalifemerchants.ui.user.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.cn.kabalifemerchants.R;
import com.cn.kabalifemerchants.base.IBaseActivity;
import com.cn.kabalifemerchants.config.LoginHelper;
import com.cn.kabalifemerchants.event.RxBus;
import com.cn.kabalifemerchants.event.RxBusBean;
import com.cn.kabalifemerchants.ui.order.fragment.OrderListFragments;
import com.cn.kabalifemerchants.ui.recruit.adapter.PagerAdapter;
import com.cn.kabalifemerchants.ui.user.bean.CashListBean;
import com.cn.kabalifemerchants.ui.user.bean.FinanceBean;
import com.cn.kabalifemerchants.ui.user.bean.ShouRuListBean;
import com.cn.kabalifemerchants.ui.user.fragment.ShouRuFragments;
import com.cn.kabalifemerchants.ui.user.fragment.TiXianFragments;
import com.cn.kabalifemerchants.utils.LoggerUtils;
import com.cn.kabalifemerchants.utils.RxToast;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/10/21.
 * function: 财务明细
 */
public class FinancialDetailsActivity extends IBaseActivity<FinancialDetailsView, FinancialDetailsPresenter> implements FinancialDetailsView, OnTabSelectListener {
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_cash_money)
    TextView tvCashMoney;
    @BindView(R.id.tv_user_cash_money)
    TextView tvUserCashMoney;
    @BindView(R.id.slitab)
    SlidingTabLayout slitab;
    @BindView(R.id.vp_type)
    ViewPager vpType;
    @BindView(R.id.tv_time)
    TextView tvTime;
    List<String> mList = new ArrayList<>();
    PagerAdapter mAdapter;
    private List<Fragment> mFragments = new ArrayList<>();
    //时间
    TimePickerView pvTime;
    private String timeUp;//时间给后台的
    String year = "";
    String month = "";

    @Override
    protected FinancialDetailsPresenter createPresenter() {
        return new FinancialDetailsPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_financial_details;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBar("财务明细");
        mPresenter.onFinanceData(LoginHelper.getLonginData().getResult());
        mList.clear();
        mList.add("提现明细");
        mList.add("收入明细");
        mFragments.add(TiXianFragments.getInstance(0));
        mFragments.add(ShouRuFragments.getInstance(1));
        mAdapter = new PagerAdapter(getSupportFragmentManager(), mList, mFragments);
        vpType.setAdapter(mAdapter);
        vpType.setOffscreenPageLimit((mList.size() - 1));
        slitab.setViewPager(vpType);
        slitab.setOnTabSelectListener(this);
    }

    @Override
    public void onTabSelect(int position) {

    }

    @Override
    public void onTabReselect(int position) {

    }

    @OnClick(R.id.ll_btn_rili)
    public void onViewClicked() {
        if (pvTime != null) {
            pvTime.show();
        } else {
            showTimeSelector();
            pvTime.show();
        }
    }

    /**
     * 选择时间
     */
    private void showTimeSelector() {
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        //startDate.set(2013,1,1);
        Calendar endDate = Calendar.getInstance();
        //endDate.set(2020,1,1);
        //正确设置方式 原因：注意事项有说明
        startDate.set(2000, 1, 1);
        endDate.set(2230, 11, 28);
        pvTime = new TimePickerBuilder(getMe(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
//                timeUp = StringUtil.getTimeAllFormat(String.valueOf(date.getTime()));
//                tvTime.setText(timeUp);
                //获取年
                year = String.valueOf(calendar.get(Calendar.YEAR));
                //获取月份，0表示1月份
                month = String.valueOf((calendar.get(Calendar.MONTH) + 1));
                LoggerUtils.e("选择的年：" + year + "-" + month);
                tvTime.setVisibility(View.VISIBLE);
                tvTime.setText(year + "-" + month);
                String time = year + "-" + month;
                RxBus.getDefault().post(new RxBusBean(300, 300, time));


            }
        })
                .setType(new boolean[]{true, true, false, false, false, false})
                .setSubmitText(getResources().getString(R.string.main_sure))
                .setCancelText(getResources().getString(R.string.main_cancel))
                .isCyclic(true)
                .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
                .setCancelColor(getResources().getColor(R.color.font_black))//取消按钮文字颜色
                .setTitleBgColor(getResources().getColor(R.color.main_white))
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)//起始终止年月日设定
                .setLabel(getResources().getString(R.string.time_year_str), getResources().getString(R.string.time_month_str), getResources().getString(R.string.time_day_str), getResources().getString(R.string.time_hours_str), getResources().getString(R.string.time_mins_str), getResources().getString(R.string.time_second_str))//默认设置为年月日时分秒
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .isDialog(false).build();
    }

    @Override
    public void onReLoggedIn(String msg) {

        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return getMe();
    }

    @Override
    public void onFinanceSuccess(FinanceBean bean) {
        tvMoney.setText(String.valueOf(bean.getResult().getMoney()));
        tvCashMoney.setText(String.valueOf(bean.getResult().getCash_money()));
        tvUserCashMoney.setText(String.valueOf(bean.getResult().getUser_cash_money()));
    }

    @Override
    public void onTiXianListSuccess(CashListBean bean) {

    }

    @Override
    public void onShouRuListSuccess(ShouRuListBean bean) {

    }
}
